
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using Apps.Models;
using Apps.Common;
using Microsoft.Practices.Unity;
using System.Transactions;
using Apps.BLL.Core;
using Apps.Locale;
using LinqToExcel;
using System.IO;
using System.Text;
using System.Linq.Expressions;
using Apps.IDAL.S;
using Apps.Models.S;
using Apps.IBLL.S;
namespace Apps.BLL.S
{
	public partial class S_COLLECTSAMPLEBLL: Virtual_S_COLLECTSAMPLEBLL,IS_COLLECTSAMPLEBLL
	{
        

	}
	public class Virtual_S_COLLECTSAMPLEBLL
	{
        [Dependency]
        public IS_COLLECTSAMPLERepository m_Rep { get; set; }

		public virtual List<S_COLLECTSAMPLEModel> GetList(ref GridPager pager, string queryStr)
        {

            IQueryable<S_COLLECTSAMPLE> queryData = null;
            if (!string.IsNullOrWhiteSpace(queryStr))
            {
                queryData = m_Rep.GetList(
								a=>a.COLLECT_ID.Contains(queryStr)
								|| a.COLLECT_CODE.Contains(queryStr)
								
								|| a.SAMPLING_CODE.Contains(queryStr)
								|| a.BATCH_CODE.Contains(queryStr)
								|| a.SAMPLE_CODE.Contains(queryStr)
								|| a.RFID.Contains(queryStr)
								
								
								|| a.S_USER_ID.Contains(queryStr)
								|| a.R_USER_ID.Contains(queryStr)
								
								
								
								);
            }
            else
            {
                queryData = m_Rep.GetList();
            }
            pager.totalRows = queryData.Count();
            //排序
            queryData = LinqHelper.SortingAndPaging(queryData, pager.sort, pager.order, pager.page, pager.rows);
            return CreateModelList(ref queryData);
        }
		 
		public virtual List<S_COLLECTSAMPLEModel> GetPagerList(ref GridPager pager, Expression<Func<S_COLLECTSAMPLE, bool>> whereLambda )
        {

            IQueryable<S_COLLECTSAMPLE> queryData = m_Rep.GetList(whereLambda); 
             
            pager.totalRows = queryData.Count();
            //排序
            queryData = LinqHelper.SortingAndPaging(queryData, pager.sort, pager.order, pager.page, pager.rows);
            return CreateModelList(ref queryData);
        }

		public virtual List<S_COLLECTSAMPLEModel> GetList(Expression<Func<S_COLLECTSAMPLE, bool>> whereLambda )
        {

            IQueryable<S_COLLECTSAMPLE> queryData = m_Rep.GetList(whereLambda); 
       
            return CreateModelList(ref queryData);
        }

		public virtual List<S_COLLECTSAMPLEModel> GetListByParentId(ref GridPager pager, string queryStr,object parentId)
        {
			return new List<S_COLLECTSAMPLEModel>();
		}

        public virtual List<S_COLLECTSAMPLEModel> CreateModelList(ref IQueryable<S_COLLECTSAMPLE> queryData)
        {

            List<S_COLLECTSAMPLEModel> modelList = (from r in queryData
                                              select new S_COLLECTSAMPLEModel
                                              {
													COLLECT_ID = r.COLLECT_ID,
													COLLECT_CODE = r.COLLECT_CODE,
													SAMPLE_TYPE = r.SAMPLE_TYPE,
													SAMPLING_CODE = r.SAMPLING_CODE,
													BATCH_CODE = r.BATCH_CODE,
													SAMPLE_CODE = r.SAMPLE_CODE,
													RFID = r.RFID,
													WEIGHT = r.WEIGHT,
													WEIGHT_STATUS = r.WEIGHT_STATUS,
													S_USER_ID = r.S_USER_ID,
													R_USER_ID = r.R_USER_ID,
													RECEIVE_TIME = r.RECEIVE_TIME,
													BLENDED = r.BLENDED,
													MADE = r.MADE,
          
                                              }).ToList();

            return modelList;
        }

        public virtual bool Create(ref ValidationErrors errors, S_COLLECTSAMPLEModel model)
        {
            try
            {
                S_COLLECTSAMPLE entity = m_Rep.GetById(model.COLLECT_ID);
                if (entity != null)
                {
                    errors.Add(Resource.PrimaryRepeat);
                    return false;
                }
                entity = new S_COLLECTSAMPLE();
               				entity.COLLECT_ID = model.COLLECT_ID;
				entity.COLLECT_CODE = model.COLLECT_CODE;
				entity.SAMPLE_TYPE = model.SAMPLE_TYPE;
				entity.SAMPLING_CODE = model.SAMPLING_CODE;
				entity.BATCH_CODE = model.BATCH_CODE;
				entity.SAMPLE_CODE = model.SAMPLE_CODE;
				entity.RFID = model.RFID;
				entity.WEIGHT = model.WEIGHT;
				entity.WEIGHT_STATUS = model.WEIGHT_STATUS;
				entity.S_USER_ID = model.S_USER_ID;
				entity.R_USER_ID = model.R_USER_ID;
				entity.RECEIVE_TIME = model.RECEIVE_TIME;
				entity.BLENDED = model.BLENDED;
				entity.MADE = model.MADE;
  

                if (m_Rep.Create(entity))
                {
                    return true;
                }
                else
                {
                    errors.Add(Resource.InsertFail);
                    return false;
                }
            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }

		
		 public virtual bool Deletes(ref ValidationErrors errors,Expression<Func<S_COLLECTSAMPLE, bool>> whereLambda)
         {
		        try
				{
					using (DBContainer db = new DBContainer())
					{
				
							foreach (var entity in db.S_COLLECTSAMPLE.Where(whereLambda))
							{
								db.Entry(entity).State = System.Data.Entity.EntityState.Deleted;
							}

							if(db.SaveChanges()>0)
							{
								return true;
							}
							else
							{
								return false;
							}
			
					}
				}
				catch (Exception ex)
				{
					errors.Add(ex.Message);
					ExceptionHander.WriteException(ex);
					return false;
				}
        }

         public virtual bool Delete(ref ValidationErrors errors, object id)
        {
            try
            {
                if (m_Rep.Delete(id) == 1)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }

        public virtual bool Delete(ref ValidationErrors errors, object[] deleteCollection)
        {
            try
            {
                if (deleteCollection != null)
                {
                    using (TransactionScope transactionScope = new TransactionScope())
                    {
                        if (m_Rep.Delete(deleteCollection) == deleteCollection.Length)
                        {
                            transactionScope.Complete();
                            return true;
                        }
                        else
                        {
                            Transaction.Current.Rollback();
                            return false;
                        }
                    }
                }
                return false;
            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }

		
       

        public virtual bool Edit(ref ValidationErrors errors, S_COLLECTSAMPLEModel model)
        {
            try
            {
                S_COLLECTSAMPLE entity = m_Rep.GetById(model.COLLECT_ID);
                if (entity == null)
                {
                    errors.Add(Resource.Disable);
                    return false;
                }
                              				entity.COLLECT_ID = model.COLLECT_ID;
				entity.COLLECT_CODE = model.COLLECT_CODE;
				entity.SAMPLE_TYPE = model.SAMPLE_TYPE;
				entity.SAMPLING_CODE = model.SAMPLING_CODE;
				entity.BATCH_CODE = model.BATCH_CODE;
				entity.SAMPLE_CODE = model.SAMPLE_CODE;
				entity.RFID = model.RFID;
				entity.WEIGHT = model.WEIGHT;
				entity.WEIGHT_STATUS = model.WEIGHT_STATUS;
				entity.S_USER_ID = model.S_USER_ID;
				entity.R_USER_ID = model.R_USER_ID;
				entity.RECEIVE_TIME = model.RECEIVE_TIME;
				entity.BLENDED = model.BLENDED;
				entity.MADE = model.MADE;
 


                if (m_Rep.Edit(entity))
                {
                    return true;
                }
                else
                {
                    errors.Add(Resource.NoDataChange);
                    return false;
                }

            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }

      

        public virtual S_COLLECTSAMPLEModel GetById(object id)
        {
            if (IsExists(id))
            {
                S_COLLECTSAMPLE entity = m_Rep.GetById(id);
                S_COLLECTSAMPLEModel model = new S_COLLECTSAMPLEModel();
                              				model.COLLECT_ID = entity.COLLECT_ID;
				model.COLLECT_CODE = entity.COLLECT_CODE;
				model.SAMPLE_TYPE = entity.SAMPLE_TYPE;
				model.SAMPLING_CODE = entity.SAMPLING_CODE;
				model.BATCH_CODE = entity.BATCH_CODE;
				model.SAMPLE_CODE = entity.SAMPLE_CODE;
				model.RFID = entity.RFID;
				model.WEIGHT = entity.WEIGHT;
				model.WEIGHT_STATUS = entity.WEIGHT_STATUS;
				model.S_USER_ID = entity.S_USER_ID;
				model.R_USER_ID = entity.R_USER_ID;
				model.RECEIVE_TIME = entity.RECEIVE_TIME;
				model.BLENDED = entity.BLENDED;
				model.MADE = entity.MADE;
 
                return model;
            }
            else
            {
                return null;
            }
        }

		public virtual S_COLLECTSAMPLEModel GetItem(Expression<Func<S_COLLECTSAMPLE, bool>> whereLambda)
        {
 
                S_COLLECTSAMPLE entity = m_Rep.GetItem(whereLambda);
                if (entity == null) return null;

                S_COLLECTSAMPLEModel model = new S_COLLECTSAMPLEModel();
                              				model.COLLECT_ID = entity.COLLECT_ID;
				model.COLLECT_CODE = entity.COLLECT_CODE;
				model.SAMPLE_TYPE = entity.SAMPLE_TYPE;
				model.SAMPLING_CODE = entity.SAMPLING_CODE;
				model.BATCH_CODE = entity.BATCH_CODE;
				model.SAMPLE_CODE = entity.SAMPLE_CODE;
				model.RFID = entity.RFID;
				model.WEIGHT = entity.WEIGHT;
				model.WEIGHT_STATUS = entity.WEIGHT_STATUS;
				model.S_USER_ID = entity.S_USER_ID;
				model.R_USER_ID = entity.R_USER_ID;
				model.RECEIVE_TIME = entity.RECEIVE_TIME;
				model.BLENDED = entity.BLENDED;
				model.MADE = entity.MADE;
 
                return model; 
        }


		 /// <summary>
        /// 校验Excel数据,这个方法一般用于重写校验逻辑
        /// </summary>
        public virtual bool CheckImportData(string fileName, List<S_COLLECTSAMPLEModel> list,ref ValidationErrors errors )
        {
          
            var targetFile = new FileInfo(fileName);

            if (!targetFile.Exists)
            {

                errors.Add("导入的数据文件不存在");
                return false;
            }

            var excelFile = new ExcelQueryFactory(fileName);

            //对应列头
			 				 excelFile.AddMapping<S_COLLECTSAMPLEModel>(x => x.COLLECT_ID, "COLLECT_ID");
				 excelFile.AddMapping<S_COLLECTSAMPLEModel>(x => x.COLLECT_CODE, "COLLECT_CODE");
				 excelFile.AddMapping<S_COLLECTSAMPLEModel>(x => x.SAMPLE_TYPE, "SAMPLE_TYPE");
				 excelFile.AddMapping<S_COLLECTSAMPLEModel>(x => x.SAMPLING_CODE, "SAMPLING_CODE");
				 excelFile.AddMapping<S_COLLECTSAMPLEModel>(x => x.BATCH_CODE, "BATCH_CODE");
				 excelFile.AddMapping<S_COLLECTSAMPLEModel>(x => x.SAMPLE_CODE, "SAMPLE_CODE");
				 excelFile.AddMapping<S_COLLECTSAMPLEModel>(x => x.RFID, "RFID");
				 excelFile.AddMapping<S_COLLECTSAMPLEModel>(x => x.WEIGHT, "WEIGHT");
				 excelFile.AddMapping<S_COLLECTSAMPLEModel>(x => x.WEIGHT_STATUS, "WEIGHT_STATUS");
				 excelFile.AddMapping<S_COLLECTSAMPLEModel>(x => x.S_USER_ID, "S_USER_ID");
				 excelFile.AddMapping<S_COLLECTSAMPLEModel>(x => x.R_USER_ID, "R_USER_ID");
				 excelFile.AddMapping<S_COLLECTSAMPLEModel>(x => x.RECEIVE_TIME, "RECEIVE_TIME");
				 excelFile.AddMapping<S_COLLECTSAMPLEModel>(x => x.BLENDED, "BLENDED");
				 excelFile.AddMapping<S_COLLECTSAMPLEModel>(x => x.MADE, "MADE");
 
            //SheetName
            var excelContent = excelFile.Worksheet<S_COLLECTSAMPLEModel>(0);
            int rowIndex = 1;
            //检查数据正确性
            foreach (var row in excelContent)
            {
                var errorMessage = new StringBuilder();
                var entity = new S_COLLECTSAMPLEModel();
						 				  entity.COLLECT_ID = row.COLLECT_ID;
				  entity.COLLECT_CODE = row.COLLECT_CODE;
				  entity.SAMPLE_TYPE = row.SAMPLE_TYPE;
				  entity.SAMPLING_CODE = row.SAMPLING_CODE;
				  entity.BATCH_CODE = row.BATCH_CODE;
				  entity.SAMPLE_CODE = row.SAMPLE_CODE;
				  entity.RFID = row.RFID;
				  entity.WEIGHT = row.WEIGHT;
				  entity.WEIGHT_STATUS = row.WEIGHT_STATUS;
				  entity.S_USER_ID = row.S_USER_ID;
				  entity.R_USER_ID = row.R_USER_ID;
				  entity.RECEIVE_TIME = row.RECEIVE_TIME;
				  entity.BLENDED = row.BLENDED;
				  entity.MADE = row.MADE;
 
                //=============================================================================
                if (errorMessage.Length > 0)
                {
                    errors.Add(string.Format(
                        "第 {0} 列发现错误：{1}{2}",
                        rowIndex,
                        errorMessage,
                        "<br/>"));
                }
                list.Add(entity);
                rowIndex += 1;
            }
            if (errors.Count > 0)
            {
                return false;
            }
            return true;
        }

        /// <summary>
        /// 保存数据
        /// </summary>
        public virtual void SaveImportData(IEnumerable<S_COLLECTSAMPLEModel> list)
        {
            try
            {
                using (DBContainer db = new DBContainer())
                {
                    foreach (var model in list)
                    {
                        S_COLLECTSAMPLE entity = new S_COLLECTSAMPLE();
                       						entity.COLLECT_ID = model.COLLECT_ID;
						entity.COLLECT_CODE = model.COLLECT_CODE;
						entity.SAMPLE_TYPE = model.SAMPLE_TYPE;
						entity.SAMPLING_CODE = model.SAMPLING_CODE;
						entity.BATCH_CODE = model.BATCH_CODE;
						entity.SAMPLE_CODE = model.SAMPLE_CODE;
						entity.RFID = model.RFID;
						entity.WEIGHT = model.WEIGHT;
						entity.WEIGHT_STATUS = model.WEIGHT_STATUS;
						entity.S_USER_ID = model.S_USER_ID;
						entity.R_USER_ID = model.R_USER_ID;
						entity.RECEIVE_TIME = model.RECEIVE_TIME;
						entity.BLENDED = model.BLENDED;
						entity.MADE = model.MADE;
 
                        db.S_COLLECTSAMPLE.Add(entity);
                    }
                    db.SaveChanges();
                }
            }
            catch
            {
                throw;
            }
        }


        public virtual bool IsExists(object id)
        {
            return m_Rep.IsExist(id);
        }
		  public void Dispose()
        { 
            
        }

	}
}
